<?php


namespace app\admin\controller;

use app\admin\model\ProjectsLayoutDrawingsDetail;
use app\facade\Upload as UploadF;
use app\service\BaiduExtractFiles;
use app\admin\model\ProjectsLayoutDrawings as ProjectsLayoutDrawingsM;

class ProjectsLayoutDrawings extends Base
{

    protected $middleware = ['app\admin\middleware\TableFields::class'];  //获取表结构，添加在返回数据中
    public $index_not_display_fields = '';  //字段不会在后台列表页，字段选择中显示

    public function filter(&$map, &$field)
    {
        //回收站
        $status = request()->get('status', 0);
        if($status == 3)
        {
            $map = [['status', '=', '3']];
        }else{
            $map = [['status', 'in', '1,2']];
        }

        $title = input('name');
        //项目订单名称
        if(!empty($title)){
            $map[] = ['name', 'like', '%'.$title.'%'];
        }

        //创建时间
        $start_time = input('start_time', 0);
        $end_time = input('end_time', 0);
        if (!empty($end_time) && !empty($start_time)) {
            $map[] = ['create_time', 'BETWEEN', [$start_time, $end_time]];
        } else if ($end_time) {
            $map[] = ['create_time', '<', $end_time];
        } else if ($start_time) {
            $map[] = ['create_time', '>', $start_time];
        }
    }

    public function finishInsert(&$data, $id)
    {
        //排料图纸关联明细
        $detail = json_decode($data['detail'], true);
        (new ProjectsLayoutDrawingsDetail())->layoutDarwReDetail($detail, $id);
    }

    public function finishUpdate($data)
    {
        //排料图纸关联明细
        $detail = json_decode($data['detail'], true);
        $detail =  empty($detail) ? [] : $detail;
        (new ProjectsLayoutDrawingsDetail())->layoutDarwReDetail($detail, $data['id'], true);

    }

    /**
     * Desc: 上传排料图纸图片-暂时弃用
     * User: 苏鹏
     * Date: 2023/10/30
     * Time: 17:05
     * @return void
     */
    public function updateImg()
    {
        $img = input('base64_img');
        try{
            $img_result = UploadF::fileBase64($img[0]);
            $img_path = $_SERVER['DOCUMENT_ROOT'].$img_result['img'];
            //调用百度提取图文api，读取数据并返回
//            $api_result = (new BaiduExtractFiles)->run($img_path);
            //api返回数据
            $api_result = '{"words_result":[{"words":"参考"},{"words":"1EQ"},{"words":"数量"},{"words":"1"},{"words":"作业"},{"words":"9月63H防护20碳钢-1-J0B002719"},{"words":"ONC"},{"words":"014470"},{"words":"1500"},{"words":"x3000x0.8"},{"words":"2023/9/22"},{"words":"用户数据3"},{"words":"14:50:49.00"},{"words":"机床"},{"words":"FSCUT-LAER"},{"words":"重圣"},{"words":"28.44"},{"words":"短"},{"words":"材质"},{"words":"ST42"},{"words":"1493.512"},{"words":"全部时间"},{"words":"05:12:32&5"},{"words":"Y"},{"words":"298.2"},{"words":"用户数据1"},{"words":"利用率)"},{"words":"90.992"},{"words":"用户数据2"},{"words":"余料)"},{"words":"93.131"},{"words":"97.703"},{"words":"#编号"},{"words":"板村"},{"words":"全部"},{"words":"缺少"},{"words":"重量"},{"words":"尺寸"},{"words":"6WC6-7234工05"},{"words":"2.47"},{"words":"1778.6x1120"},{"words":"74WC623红0☒"},{"words":"2008"},{"words":"60"},{"words":"x30"},{"words":"7WC67234"},{"words":"24"},{"words":"12兴.华5x30"},{"words":"2▣"},{"words":"12.6x30"},{"words":"0"},{"words":"0.47生"},{"words":".6x100"},{"words":"Q.4日"},{"words":"1288x96"},{"words":"0.048"},{"words":"12.x9E"},{"words":"W637284-0日"},{"words":"Q.049"},{"words":"双"},{"words":"x116.2"},{"words":"0.02"},{"words":"149"},{"words":"x10.2"},{"words":"77VC6-74红04"},{"words":"0"},{"words":"0.03"},{"words":"7.405x0.058"},{"words":"78WC红0"},{"words":"a.03雨"},{"words":"."},{"words":"268"},{"words":"2026"},{"words":"x0.2"},{"words":"4日WC67233202"},{"words":"0.19"},{"words":"09"},{"words":"x103.2"},{"words":"9"},{"words":"x2五.1"},{"words":"10WC67203"},{"words":"0.￥6日"},{"words":"1WC6-1L-13"},{"words":"0,4里"},{"words":"37.3"},{"words":"x1462"},{"words":"0"},{"words":"0.08g"},{"words":"6204x2五12"},{"words":")0"},{"words":"T"},{"words":"工"}],"words_result_num":92,"log_id":1719601741809828345}';
            $api_result = json_decode($api_result, true);
            if(empty($api_result['words_result']))
            {
                re_error_json(msg_code(29)); //图片未提取到文字信息
            }else{
                //加工图文信息
                (new ProjectsLayoutDrawingsM())->addLayoutDraw($api_result);
            }
        }catch (\Exception $e){
            return re_error_json($e->getMessage());
        }
        return re_success_json();
    }

}